﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using BrainTechLLC.EmlenMud.Communication;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using System.Net;
using System.Threading;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using BrainTechLLC.Communications;
using BrainTechLLC.WebCamLib3;
using BrainTechLLC.ImageOperators;

namespace WpfImageReceiver
{
    public class VideoFeedProvider : BaseNotifyPropertyChanged
    {
        protected GenericVideoFeed _videoFeed;
        protected Communications _communications;
        protected Thread _workerThread;
        protected bool _workerThreadWantExit;

        public VideoFeedProvider(Communications communications, GenericVideoFeed videoFeed)
        {
            _communications = communications;
            _communications.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_communications_PropertyChanged);
            _videoFeed = videoFeed;
            CheckConnected();
        }

        private void CheckConnected()
        {
            if (_communications.Connected)
            {
                while (_workerThread != null)
                {
                    _workerThreadWantExit = true;
                    Thread.Sleep(300);
                }
                ThreadStart ts = new ThreadStart(WorkerThread);
                _workerThread = ts.CreateAndRunThread(true);
            }
            else
            {
                _workerThreadWantExit = true;
            }
        }

        void _communications_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Connected")
            {
                if (_communications.Connected)
                {
                    while (_workerThread != null)
                    {
                        _workerThreadWantExit = true;
                        Thread.Sleep(300);
                    }
                    ThreadStart ts = new ThreadStart(WorkerThread);
                    _workerThread = ts.CreateAndRunThread(true);
                }
                else
                {
                    _workerThreadWantExit = true;
                }
            }
        }

        public void WorkerThread()
        {
            while (!_workerThreadWantExit)
            {
                Bitmap bm;

                if (_communications.IncomingBitmapQueue.Dequeue(out bm))
                {
                    FrameInfo frame = new FrameInfo();
                    frame._bytes = bm.CreateBytes();
                          frame.ByteLength= frame._bytes.Length;
                    frame.CameraMoniker = string.Empty;
                    frame.CameraName=string.Empty;
                    frame.Height =bm.Height;
                    frame.Width = bm.Width;
                    frame.PixelFormat = bm.PixelFormat;
                    frame.ImageFrameTime = (double)DateTime.Now.Ticks;
                    frame.StrideGuess =  (frame.Width * 3) + ((frame.Width * 3) % 4);

                    if (_videoFeed.IncomingVideoHeight != frame.Height || _videoFeed.IncomingVideoWidth != frame.Width)
                    {
                        _videoFeed._incomingVideoHeight = frame.Height;
                        _videoFeed._incomingVideoWidth = frame.Width;
                    }

                    _videoFeed.IncomingFrameQueue.Enqueue(frame);
                }
                else
                {
                    Thread.Sleep(30);
                }
            }
            _workerThread = null;
        }        
    }
}
